package ch.ffhs.contacts;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * class ContactListAdapter. Defines a list-adapter for displaying contacts with
 * a checkbox in a list-view.
 * 
 * @author mf
 */
public class ContactsAlarmListAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<Contact> contacts;

	public ContactsAlarmListAdapter(Context context, ArrayList<Contact> persons) {
		this.context = context;
		this.contacts = persons;
	}

	@Override
	public int getCount() {
		return contacts.size();
	}

	@Override
	public Object getItem(int position) {
		return contacts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		LinearLayout item;

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			item = (LinearLayout) inflater.inflate(
					ch.ffhs.activities.R.layout.contact_alarm_list_item, null);
		} else {
			item = (LinearLayout) convertView;
		}

		TextView text1 = (TextView) item
				.findViewById(ch.ffhs.activities.R.id.text1);
		TextView text2 = (TextView) item
				.findViewById(ch.ffhs.activities.R.id.text2);
		CheckBox checkBox = (CheckBox) item
				.findViewById(ch.ffhs.activities.R.id.checked);

		Contact contact = contacts.get(position);
		text1.setText(contact.getName());
		text2.setText("" + contact.getNumber());
		checkBox.setChecked(contact.getChecked());

		return item;
	}
}
